<?php
require 'lib/init.php';
if (empty($_POST)) {
    $res = mQuery('select * from cat;');
    require 'view/admin/artadd.html';
} else {
    if (empty(trim($_POST['title']))) {
        echo '标题不能为空';
        header('refresh:2;url=artadd.php');
        exit();
    }
    if (empty(trim($_POST['content']))) {
        echo '内容不能为空';
        header('refresh:2;url=artadd.php');
        exit();
    }
    mQuery("set names utf8");

//    图片上传
    if (!empty($_FILES) && ($_FILES['pic']['error'] == 0)) {
        if($path=uploadFile('pic')){
            $_POST['pic']=$path;
//            echo ROOT.$path;
            $_POST['thumb_path']=thumb(ROOT.$path);
        }else{
            error('图片上传失败');
        }
    }

    $_POST['pubtime'] = time();
    $res = mExec('article', $_POST);
//    $res?success('文章插入成功'):error('文章插入失败');
    if ($res) {
        mQuery("update cat set num=num+1 where cat_id=$_POST[cat_id];");
        success('文章添加成功');
    } else {
        error('文章插入失败');
    }
}
